package com.example.mask.maskmobilesshop.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.example.mask.maskmobilesshop.entity.Index;
import com.example.mask.maskmobilesshop.entity.Member;
import com.example.mask.maskmobilesshop.enums.HttpStatus;
import com.example.mask.maskmobilesshop.enums.Types;
import com.example.mask.maskmobilesshop.service.IMemberService;
import com.example.mask.maskmobilesshop.service.IndexService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;


/**
 * index
 * @author anhuifix
 * @email anhuifix@163.com
 */


@RestController
@RequestMapping(value = "/index")
public class IndexController {

    private Logger logger = LoggerFactory.getLogger(LoginController.class.getName());

    @Autowired
    private IndexService indexService;

    @Autowired
    private IMemberService memberService;




    @ApiOperation(value = "获取主页信息接口", notes = "返回: 2000-成功 1000-会员不存在 3000-参数传入不正确 5000-服务器异常")
    @ApiImplicitParams({@ApiImplicitParam(name = "mobile", value = "用户手机号码", required = true, dataType = "String", paramType = "query")})
    @RequestMapping(value = "/init", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HashMap<String,Object> init(String mobile){
        HashMap<String,Object> result = new HashMap<>();
        try {
            if(!StringUtils.isEmpty(mobile)){
                Member dbMember = memberService.findByName(mobile);
                        if(dbMember != null){
                            List<Index> banner = indexService.selectList(new EntityWrapper<Index>().eq("type", Types.APP_INDEX_TYPE_BANNER.getIndex()).orderBy("sort", true));
                            List<Index> navs = indexService.selectList(new EntityWrapper<Index>().eq("type", Types.APP_INDEX_TYPE_NAV.getIndex()).orderBy("sort", true));
                            result.put("banner",banner);
                            result.put("navs",navs);
                            result.put("status", HttpStatus.SUCCESS.getValue());
                        }else{
                            result.put("status", HttpStatus.MEMBER_NOT_EXIST.getValue());
                        }

            }else{
                result.put("status", HttpStatus.BAD_PARAMETERS.getValue());
            }
        }catch (Exception e){
            result.put("status", HttpStatus.INTERNAL_SERVER_ERROR.getValue());
            logger.error("初始化APP首页展示内容时异常, 错误堆栈轨迹如下: ");
            e.printStackTrace();
        }
        return  result;
    }


}
